﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
//在此处加上log4net注册信息，用于发布web项目到IIS上面后，出错过程会记录日志，用于开发人员检查错误原因
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
namespace ParkService
{
    public class Logger
    {
        private Logger()
        {

        }

        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");

        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

        public static readonly log4net.ILog logdebug = log4net.LogManager.GetLogger("logdebug");

        public static Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        private static EndPoint serverPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60150);

        public static void SetConfig()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        public static void SetConfig(FileInfo configFile)
        {
            log4net.Config.XmlConfigurator.Configure(configFile);
        }

        public static void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
            byte[] data = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("HH:mm:ss") + " : " + info + "\r\n");
            udpClient.SendTo(data, serverPoint);
        }

        public static void WriteError(string info, Exception se)
        {
            if (logerror.IsErrorEnabled)
            {
                logerror.Error(info, se);
            }
        }
        public static void WriteDebug(string info)
        {
            if (logdebug.IsDebugEnabled)
            {
                logdebug.Debug(info);
            }
        }
    }
}